<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Register</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php
include("global.php");

if(isset ($_POST["user"]) && isset ($_POST["pass"]) && isset ($_POST["repass"]))
{
    $u=$_POST["user"];
    $p=md5($_POST["pass"]);
    $rp=md5($_POST["repass"]);
 	$e=$_POST["email"];
	$re=$_POST["reemail"];
    if($p==$rp)
    {
        if($e==$re)
        {
            $result=@mysql_query("SELECT Username From USERS WHERE Username ='".$u."'",$conn);
            $row=@mysql_fetch_row($result);
            if(count($row)>1)
                echo  "Tài khoản đã tồn tại !";
            else
                $sql="INSERT INTO USERS(Username,Password,Email) Values('".$u."','".$p."','".$e."')";
                if(@mysql_query($sql,$conn))
                    echo "Thêm thành công !";
        }
        else
            echo "Ðịa chỉ Email không trùng khớp !";
    }
    else
        echo "Mật khẩu không trùng khớp !";
}
?>
<h1>Register</h1>
<script type="text/javascript">
function CheckUser(username)
{
    var xmlhttp;
    if(Window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest();
    }
    else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if(xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("user_check").innerHTML=xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","CheckUser.php?user=username",true);
    xmlhttp.send();
}
function PassStrong(pass){}
function CheckPass(CfPass)
{
    CfPass=CfPass.value;    
    var pw=document.getElementById("pass").value;
    if(CfPass==pw)
        document.getElementById("pass_check").innerHTML="";
    else
        document.getElementById("pass_check").innerHTML="Mật khẩu không khớp !";
}
function EmailValid(email)
{
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(email.value)) {
        document.getElementById("email_valid").innerHTML="Địa chỉ email không hợp lệ !";
        email.focus;
        document.getElementById("smbt").disable;
    }
    else
        document.getElementById("email_valid").innerHTML="";
}
function CheckEmail(CfEmail)
{
    CfEmail=CfEmail.value;    
    var em=document.getElementById("email").value;
    if(CfEmail==em)
        document.getElementById("email_check").innerHTML="";
    else
        document.getElementById("email_check").innerHTML="Địa chỉ email không khớp !";
}
</script>
<form id="register" name="register" method="post" action="member.php?mod=register">
<table>
<tr><td>Enter Username:&nbsp;</td><td><input type="text" name="user" id="user" size="30" onblur="CheckUser(this)" /></td><td><span class="error" id="user_check"></span></td></tr>
<tr><td>Enter Password:&nbsp;</td><td><input type="password" name="pass" id="pass" size="30" /></td><td><span id="pass_str" class="error"></span></td></tr>
<tr><td>Confirm password:&nbsp;</td><td><input type="password" name="repass" id="repass" size="30" onblur="CheckPass(this)" /></td><td><span id="pass_check" class="error"></span></td></tr>
<tr><td>Email Address: &nbsp;</td><td><input type="text" name="email" id="email" size="30" onblur="EmailValid(this)" /></td><td><span id="email_valid" class="error"></span></td></tr>
<tr><td>Confirm Email: &nbsp;</td><td><input type="text" name="reemail" id="reemail" size="30" onblur="CheckEmail(this)" /></td><td><span id="email_check" class="error"></span></td></tr>	  
<tr><td colspan="3"><input type="submit" name="submit" id="smbt" value="Register" onsubmit="if (this.getAttribute('submitted')) return false;" /></td></tr>
</table>
</form>

</body>
</html>
